package cn.gov.pbc.address.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.excel.EasyExcel;

import cn.gov.pbc.address.common.ServerResponse;
import cn.gov.pbc.address.dao.AddressMapper;
import cn.gov.pbc.address.excel.AddressExcelListener;
import cn.gov.pbc.address.excel.AddressExcelRow;
import cn.gov.pbc.address.pojo.Address;
import cn.gov.pbc.address.service.IAddressService;
import lombok.extern.slf4j.Slf4j;

@Service("iAddressService")
@Slf4j
public class AddressServiceImpl implements IAddressService {

	@Autowired
	private AddressMapper addressMapper;

	@Override
	public void addAddress() {
		this.addressMapper.deleteAll();
		String fileName = "C:\\project\\address\\bankinfo.xls";

		AddressExcelListener listener = new AddressExcelListener();
		EasyExcel.read(fileName, AddressExcelRow.class, listener).sheet(0).doRead();
		List<Address> addresseList = listener.getRowList();
		log.info("共读取{}条数据", listener.getRowList().size());
		int pageSize = 10;
		int pageCount = addresseList.size() / pageSize;
		if (addresseList.size() % pageSize != 0) {
			pageCount++;
		}
		for (int i = 0; i < pageCount; i++) {
			int start = i * pageSize;
			int end = start + pageSize;
			if (end >= addresseList.size()) {
				end = addresseList.size();
			}
			List<Address> list = addresseList.subList(start, end);
			this.addressMapper.insertBatch(list);
		}
	}

	@Override
	public ServerResponse<?> selectAll() {
		List<Address> addresseList = this.addressMapper.selectAll();
		return ServerResponse.createBySuccess(addresseList);
	}
	
	@Override
	public ServerResponse<?> updateLngLat(Integer id, BigDecimal lng, BigDecimal lat){
		Address address = new Address();
		address.setId(id);
		address.setLng(lng);
		address.setLat(lat);
		int rowCount = this.addressMapper.updateByPrimaryKeySelective(address);
		if(rowCount == 0) {
			return ServerResponse.createByErrorMessage("经纬度更新失败");
		}
		return ServerResponse.createBySuccessMessage("更新成功");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
